{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class ListNode:\n",
    "    def __init__(self, val=None, next=None):\n",
    "        self.data = val\n",
    "        self.next = next\n",
    "class LinkedList:\n",
    "    def __init__(self):\n",
    "        self.dummy_head = ListNode()\n",
    "        self.size = 0\n",
    "        \n",
    "    def create(self, List):\n",
    "        for val in List:\n",
    "            last_node = self.dummy_head\n",
    "            while last_node.next:\n",
    "                last_node = last_node.next\n",
    "            last_node.next = ListNode(val)\n",
    "            self.size +=1\n",
    "            \n",
    "    def circle_list(self, index):\n",
    "        if index < 0 or index > self.size-1:\n",
    "            return\n",
    "        cur = self.dummy_head\n",
    "        for _ in range(index):\n",
    "            cur = cur.next\n",
    "        last = self.dummy_head    \n",
    "        for _ in range(self.size):\n",
    "            last = last.next\n",
    "        last.next = cur\n",
    "        \n",
    "    def print_circle(self):\n",
    "        cur = self.dummy_head.next\n",
    "        visited = set()                     #python中set是一个无序集合，可以用于存储不重复元素，即列表里有的元素添加将不会造成任何操作\n",
    "        while cur:\n",
    "            print(cur.data, end=\" -> \")\n",
    "            if cur in visited:\n",
    "                print(\"Circle\")\n",
    "                return\n",
    "            visited.add(cur)\n",
    "            cur = cur.next\n",
    "        print(\"Circle\")\n",
    "        \n",
    "    def algorithm(self):\n",
    "        slow = self.dummy_head.next\n",
    "        fast = self.dummy_head.next\n",
    "        while fast and fast.next:\n",
    "            slow = slow.next\n",
    "            fast = fast.next.next\n",
    "            if slow == fast:\n",
    "                slow = self.dummy_head.next\n",
    "                while slow != fast:\n",
    "                    slow = slow.next\n",
    "                    fast = fast.next\n",
    "                return slow.data\n",
    "        return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "LinkedList = LinkedList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = list(range(6))\n",
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 2 -> Circle\n"
     ]
    }
   ],
   "source": [
    "LinkedList.create(nums)\n",
    "LinkedList.circle_list(3)\n",
    "LinkedList.print_circle()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LinkedList.algorithm()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
